package magicloan.auctionpocket.ui.activity;

import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;

import java.util.List;

import kr.co.namee.permissiongen.PermissionFail;
import kr.co.namee.permissiongen.PermissionGen;
import kr.co.namee.permissiongen.PermissionSuccess;
import magicloan.auctionpocket.R;
import magicloan.auctionpocket.ui.base.BaseActivity;
import me.iwf.photopicker.PhotoPicker;
import me.iwf.photopicker.PhotoPreview;

/**
 * Created by hechao on 2018/2/1.
 */
public class PersonActivity extends BaseActivity {

    private ImageView mImg ;

    @Override
    protected int setContentView() {
        return R.layout.ac_person;
    }

    @Override
    protected void beforeInitView() {

    }

    @Override
    protected void afterInitView() {

        setTitle("个人信息",null);

        findViewById(R.id.ll_name).setOnClickListener(this);

        mImg = (ImageView) findViewById(R.id.img);
        mImg.setOnClickListener(this);

        Glide.with(mContext)
                .load("")
                .placeholder(R.mipmap.icon_user_default_large)
                .crossFade()
                .into(mImg);

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {

    }

    @Override
    public void onClick(View v) {
        Intent intent = null ;
        super.onClick(v);
        switch (v.getId()){
            case R.id.ll_name:
                intent = new Intent(mActivity,SettingNameActivity.class);
                break;
            case R.id.img:
                showOptions();
                break;
        }
        if ( intent!=null )
            startActivity(intent);
    }

    public void showOptions() {
        PhotoPicker.builder().checkPermission(mActivity);
    }

    @PermissionSuccess(requestCode = 100)
    public void camera_success() {
        PhotoPicker.builder().setPhotoCount(1).start(this);
    }

    @PermissionFail(requestCode = 100)
    private void camera_fail() {
        Toast.makeText(this, "请打开相机拍照及读写内存卡的权限", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions,
                                           int[] grantResults) {
        PermissionGen.onRequestPermissionsResult(this, requestCode, permissions, grantResults);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (resultCode == RESULT_OK && (requestCode == PhotoPicker.REQUEST_CODE || requestCode == PhotoPreview.REQUEST_CODE)) {
            List<String> photos = null;
            if (intent != null) {
                photos = intent.getStringArrayListExtra(PhotoPicker.KEY_SELECTED_PHOTOS);
                Log.d("photos", photos.get(0) + "," + Uri.parse(photos.get(0)));
                Glide.with(mContext)
                        .load(photos.get(0))
                        .placeholder(R.mipmap.icon_user_default_large)
                        .crossFade()
                        .into(mImg);
//                loadingDialog.show();
//                pushImg(photos.get(0));
            }
        }
    }

}
